// pages/myCollection/myCollection.js

  var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    collectArticleList:[],
    total: 1,
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;

    that.getCollectArticleList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 我们用total和count来控制分页，total代表已请求数据的总数，count代表每次请求的个数。
    // 刷新时需把total重置为0，代表重新从第一条请求。
    that.data.total = 1;
    // this.data.dataArray 是页面中绑定的数据源
    that.data.collectArticleList = [];


    // 网络请求，重新请求一遍数据
    that.getCollectArticleList();
    // 小程序提供的api，通知页面停止下拉刷新效果

    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
    that.data.total = that.data.total + 1;
    setTimeout(() => {
      that.getCollectArticleList();
      wx.stopPullDownRefresh();
    }, 1500)
  },

  getCollectArticleList:function(){
    wx.request({
      url: getApp().globalData.urls + '/wxgz/articleManage/getCollectArticleList',
      data: {
        token: wx.getStorageSync("token")
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded' // post:application/x-www-form-urlencoded,get:application/json
      },
      success: function (res) {
        console.log(res.data)
        

        if(res.data.code == 200){
        var datas = res.data.data;
        
        for (let i = 0; i < datas.length; i++) {
          //datas[i].created = that.jsonTimeStamp(datas[i].created)

          var datetime = new Date();
          datetime.setTime(datas[i].releaseTime);
          var year = datetime.getFullYear();
          var month = datetime.getMonth() + 1 < 10 ? "0"
            + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
          var date = datetime.getDate() < 10 ? "0" + datetime.getDate()
            : datetime.getDate();
          var hour = datetime.getHours() < 10 ? "0" + datetime.getHours()
            : datetime.getHours();
          var minute = datetime.getMinutes() < 10 ? "0"
            + datetime.getMinutes() : datetime.getMinutes();
          var second = datetime.getSeconds() < 10 ? "0"
            + datetime.getSeconds() : datetime.getSeconds();
          datas[i].releaseTime = year + "-" + month + "-" + date + " " + hour + ":" + minute
            + ":" + second;
          }

        var list = that.data.collectArticleList;
        for (var i = 0; i < datas.length; i++) {
          list.push(datas[i]);
        }

          that.setData({
            collectArticleList: datas
          })
        }
      
      },
      fail: function (res) {

      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  
  //将时间撮格式的日期转化为标准的格式
  jsonTimeStamp:function(milliseconds) {
    if(milliseconds != "" && milliseconds != null
      && milliseconds != "null") {
      var datetime = new Date();
      datetime.setTime(milliseconds);
      var year = datetime.getFullYear();
      var month = datetime.getMonth() + 1 < 10 ? "0"
        + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
      var date = datetime.getDate() < 10 ? "0" + datetime.getDate()
        : datetime.getDate();
      var hour = datetime.getHours() < 10 ? "0" + datetime.getHours()
        : datetime.getHours();
      var minute = datetime.getMinutes() < 10 ? "0"
        + datetime.getMinutes() : datetime.getMinutes();
      var second = datetime.getSeconds() < 10 ? "0"
        + datetime.getSeconds() : datetime.getSeconds();
      return year + "-" + month + "-" + date + " " + hour + ":" + minute
        + ":" + second;
    } else {
      return "";
    }

  },



  aaaa:function(e){
    console.log(e.currentTarget.dataset.item)
    var data = e.currentTarget.dataset.item
    var id = data.id
    //var id= 24
    //let data = JSON.stringify(e.currentTarget.dataset.data); 
    //console.log(data)
    wx.navigateTo({
      url: '../articleDetail/articleDetail?id=' + id
    })
  }
})